package com.gxar.quick.ar.server.base.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.warning.ApiLog;
import com.gxar.quick.ar.server.base.common.properties.UserProperties;
import com.gxar.quick.ar.server.base.facade.UserFacade;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.user.vo.request.ChangeConfigRequest;
import com.gxar.quick.ar.server.base.model.user.vo.request.ComboBuyRequest;
import com.gxar.quick.ar.server.base.model.user.vo.request.FeedbackRequest;
import com.gxar.quick.ar.server.base.model.user.vo.response.*;
import com.gxar.quick.ar.server.db.service.ExchangeItemService;
import com.gxar.quick.ar.server.db.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/7 14:50
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    private final UserFacade userFacade;

    private final ExchangeItemService exchangeItemService;

    private final UserProperties userProperties;

    @ApiOperation("会员过期")
    @GetMapping("/member-expired")
    public void memberExpired() {
        userService.userMemberExpired();
    }

    @ApiOperation("素材过期")
    @GetMapping("/material-expired")
    public void materialExpired(@RequestParam(value = "date_time", required = false) String dateTime) {
        DateTime yesterday = DateUtil.yesterday();
        if (StringUtils.isNotBlank(dateTime)) {
            yesterday = DateUtil.parse(dateTime, "yyyy-MM-dd");
        }
        DateTime startTime = DateUtil.beginOfDay(yesterday);
        DateTime endTime = DateUtil.beginOfDay(new Date());
        userService.materialExpired(startTime, endTime);
    }

    @ApiOperation("用户信息查询")
    @GetMapping("/info")
    public Result<UserInfoResponse> userInfo() {
        UserProfile userProfile = LoginUserThread.get();
        UserInfoResponse userInfoResponse = userService.userInfo(userProfile);
        return Result.success(userInfoResponse);
    }

    @ApiOperation("用户授权")
    @PostMapping("/authorization")
    public Result<UserAuthorizationResponse> authorization() {
        UserProfile userProfile = LoginUserThread.get();
        Result<UserAuthorizationResponse> authorization = userService.authorization(userProfile);
        // 领取兑换品
        exchangeItemService.receiveExchangeItem(userProfile);
        return authorization;
    }

    @ApiOperation("查询用户权益")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "items",
                    value = "查询权益的item，英文逗号隔开，不传默认查全部，Slot-坑位，Identify-识别图大小，Video-识别图大小，" +
                            "3DModel-3D模型大小, Impressions-浏览次数。例如：item=Slot,ImpressionsNumber",
                    dataType = "string"),
    })
    @GetMapping("/rights")
    public Result<UserRightsResponse> userRights(@RequestParam(value = "items", required = false) String items) {
        UserProfile userProfile = LoginUserThread.get();
        UserRightsResponse response = userService.userRights(userProfile, items);
        return Result.success(response);
    }

    @ApiOperation("购买会员")
    @PostMapping("/combo/buy")
    @ApiLog
    public Result<ComboBuyResponse> comboBuy(@RequestBody ComboBuyRequest comboBuyRequest) {
        UserProfile userProfile = LoginUserThread.get();
        return userFacade.comboBuy(userProfile, comboBuyRequest);
    }

    @ApiOperation("意见反馈")
    @PostMapping("/feedback")
    public Result<FeedbackResponse> feedback(@RequestBody FeedbackRequest feedbackRequest) {
        UserProfile userProfile = LoginUserThread.get();
        FeedbackResponse feedback = userService.feedback(userProfile, feedbackRequest);
        return Result.success(feedback);
    }

    @ApiOperation("校验用户权益")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "items",
                    value = "查询权益的item，英文逗号隔开，不传默认查全部，Slot-坑位，MaterialSize-素材大小，ImpressionsNumber-浏览次数。例如：item=Slot,ImpressionsNumber",
                    dataType = "string"),
    })
    @GetMapping("/verify-rights")
    public Result<VerifyRightsResponse> verifyUserRights(@RequestParam(value = "items", required = false) String items) {
        UserProfile userProfile = LoginUserThread.get();
        VerifyRightsResponse response = userService.verifyUserRights(userProfile, items);
        return Result.success(response);
    }

    @ApiOperation("使用（用户首次使用，点我知道了调用）")
    @PostMapping("/use")
    public Result<UserUseResponse> use() {
        UserProfile userProfile = LoginUserThread.get();
        return userService.use(userProfile);
    }

}
